<template>
  <div class="resume-container">
    <el-row :gutter="20">
      <!-- 左侧编辑区域 -->
      <el-col :span="8">
        <el-card class="edit-card">
          <template #header>
            <div class="card-header">
              <span>简历信息编辑</span>
              <el-button type="primary" @click="generateResume">生成简历</el-button>
            </div>
          </template>

          <el-form :model="resumeForm" label-width="80px">
            <!-- 基本信息 -->
            <el-divider>基本信息</el-divider>
            <el-form-item label="姓名">
              <el-input v-model="resumeForm.name" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="职位">
              <el-input v-model="resumeForm.position" placeholder="期望职位" />
            </el-form-item>
            <el-form-item label="联系方式">
              <el-input v-model="resumeForm.contact" placeholder="电话/邮箱" />
            </el-form-item>

            <!-- 教育背景 -->
            <el-divider>教育背景</el-divider>
            <div v-for="(edu, index) in resumeForm.education" :key="index" class="dynamic-item">
              <el-form-item :label="'教育 ' + (index + 1)">
                <el-input v-model="edu.school" placeholder="学校名称" />
                <el-input v-model="edu.major" placeholder="专业" class="mt-2" />
                <el-date-picker
                  v-model="edu.time"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  class="mt-2 w-full"
                />
                <el-button type="danger" link @click="removeEducation(index)" class="mt-2">
                  删除
                </el-button>
              </el-form-item>
            </div>
            <el-button type="primary" link @click="addEducation">添加教育经历</el-button>

            <!-- 工作经验 -->
            <el-divider>工作经验</el-divider>
            <div v-for="(exp, index) in resumeForm.experience" :key="index" class="dynamic-item">
              <el-form-item :label="'经验 ' + (index + 1)">
                <el-input v-model="exp.company" placeholder="公司名称" />
                <el-input v-model="exp.position" placeholder="职位" class="mt-2" />
                <el-date-picker
                  v-model="exp.time"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  class="mt-2 w-full"
                />
                <el-input
                  v-model="exp.description"
                  type="textarea"
                  placeholder="工作描述"
                  :rows="3"
                  class="mt-2"
                />
                <el-button type="danger" link @click="removeExperience(index)" class="mt-2">
                  删除
                </el-button>
              </el-form-item>
            </div>
            <el-button type="primary" link @click="addExperience">添加工作经验</el-button>

            <!-- 技能特长 -->
            <el-divider>技能特长</el-divider>
            <el-form-item label="技能">
              <el-select
                v-model="resumeForm.skills"
                multiple
                filterable
                allow-create
                default-first-option
                placeholder="请输入技能标签"
                class="w-full"
              >
                <el-option
                  v-for="skill in skillOptions"
                  :key="skill"
                  :label="skill"
                  :value="skill"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>

      <!-- 右侧预览区域 -->
      <el-col :span="16">
        <el-card class="preview-card">
          <template #header>
            <div class="card-header">
              <span>简历预览</span>
              <el-button type="success" @click="downloadPDF">下载 PDF</el-button>
            </div>
          </template>

          <div class="resume-preview" ref="resumePreview">
            <div class="resume-header">
              <h1>{{ resumeForm.name || "姓名" }}</h1>
              <p>{{ resumeForm.position || "期望职位" }}</p>
              <p>{{ resumeForm.contact || "联系方式" }}</p>
            </div>

            <div class="resume-section">
              <h2>教育背景</h2>
              <div v-for="(edu, index) in resumeForm.education" :key="index" class="section-item">
                <h3>{{ edu.school }}</h3>
                <p>{{ edu.major }}</p>
                <p v-if="edu.time?.length">
                  {{ formatDate(edu.time[0]) }} - {{ formatDate(edu.time[1]) }}
                </p>
              </div>
            </div>

            <div class="resume-section">
              <h2>工作经验</h2>
              <div v-for="(exp, index) in resumeForm.experience" :key="index" class="section-item">
                <h3>{{ exp.company }}</h3>
                <p>{{ exp.position }}</p>
                <p v-if="exp.time?.length">
                  {{ formatDate(exp.time[0]) }} - {{ formatDate(exp.time[1]) }}
                </p>
                <p class="description">{{ exp.description }}</p>
              </div>
            </div>

            <div class="resume-section">
              <h2>技能特长</h2>
              <div class="skills-container">
                <el-tag v-for="skill in resumeForm.skills" :key="skill" class="skill-tag">
                  {{ skill }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import html2pdf from "html2pdf.js";
import { ElMessage } from "element-plus";

// 简历表单数据
const resumeForm = reactive({
  name: "",
  position: "",
  contact: "",
  education: [{ school: "", major: "", time: [] }],
  experience: [{ company: "", position: "", time: [], description: "" }],
  skills: [],
});

// 预设技能选项
const skillOptions = [
  "JavaScript",
  "TypeScript",
  "Vue",
  "React",
  "Node.js",
  "Python",
  "Java",
  "C++",
  "MySQL",
  "MongoDB",
  "Docker",
  "Git",
  "Linux",
  "AWS",
  "Azure",
];

// 添加/删除教育经历
const addEducation = () => {
  resumeForm.education.push({ school: "", major: "", time: [] });
};

const removeEducation = (index: number) => {
  resumeForm.education.splice(index, 1);
};

// 添加/删除工作经验
const addExperience = () => {
  resumeForm.experience.push({ company: "", position: "", time: [], description: "" });
};

const removeExperience = (index: number) => {
  resumeForm.experience.splice(index, 1);
};

// 格式化日期
const formatDate = (date: Date) => {
  if (!date) return "";
  return new Date(date).toLocaleDateString("zh-CN", {
    year: "numeric",
    month: "2-digit",
  });
};

// 生成简历
const generateResume = () => {
  ElMessage.success("简历生成成功！");
};

// 下载 PDF
const resumePreview = ref<HTMLElement>();
const downloadPDF = async () => {
  if (!resumePreview.value) return;

  const opt = {
    margin: 1,
    filename: `${resumeForm.name || "resume"}.pdf`,
    image: { type: "jpeg", quality: 0.98 },
    html2canvas: { scale: 2 },
    jsPDF: { unit: "in", format: "a4", orientation: "portrait" },
  };

  try {
    await html2pdf().set(opt).from(resumePreview.value).save();
    ElMessage.success("PDF 下载成功！");
  } catch (error) {
    ElMessage.error("PDF 生成失败，请重试！");
  }
};
</script>

<style scoped>
.resume-container {
  padding: 24px;
  background-color: var(--el-bg-color-page);
  min-height: calc(100vh - 84px);
}

.edit-card,
.preview-card {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.edit-card:hover,
.preview-card:hover {
  box-shadow: var(--el-box-shadow-light);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 4px;
}

.dynamic-item {
  border: 1px solid var(--el-border-color-lighter);
  padding: 20px;
  margin-bottom: 20px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background-color: var(--el-bg-color);
}

.dynamic-item:hover {
  border-color: var(--el-color-primary-light-7);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.mt-2 {
  margin-top: 12px;
}

.w-full {
  width: 100%;
}

/* 简历预览样式 */
.resume-preview {
  padding: 40px;
  background: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  max-width: 800px;
  margin: 0 auto;
}

.resume-header {
  text-align: center;
  margin-bottom: 40px;
  padding-bottom: 20px;
  border-bottom: 2px solid var(--el-color-primary-light-8);
}

.resume-header h1 {
  margin: 0 0 16px;
  color: var(--el-color-primary);
  font-size: 32px;
  font-weight: 600;
}

.resume-header p {
  margin: 8px 0;
  color: var(--el-text-color-regular);
  font-size: 16px;
}

.resume-section {
  margin-bottom: 35px;
}

.resume-section h2 {
  color: var(--el-color-primary);
  font-size: 24px;
  font-weight: 500;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--el-color-primary-light-5);
}

.section-item {
  margin-bottom: 25px;
  padding: 15px;
  border-radius: 6px;
  background-color: var(--el-bg-color-page);
  transition: all 0.3s ease;
}

.section-item:hover {
  transform: translateX(5px);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.section-item h3 {
  margin: 0 0 8px;
  color: var(--el-color-primary-dark-2);
  font-size: 18px;
  font-weight: 500;
}

.section-item p {
  margin: 8px 0;
  color: var(--el-text-color-regular);
  line-height: 1.6;
}

.description {
  white-space: pre-line;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  line-height: 1.8;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--el-border-color-lighter);
}

.skills-container {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 15px;
  background-color: var(--el-bg-color-page);
  border-radius: 6px;
}

.skill-tag {
  margin: 0;
  padding: 8px 16px;
  font-size: 14px;
  border-radius: 4px;
  background-color: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
  border: none;
  transition: all 0.3s ease;
}

.skill-tag:hover {
  transform: translateY(-2px);
  background-color: var(--el-color-primary-light-8);
}

:deep(.el-divider__text) {
  color: var(--el-text-color-primary);
  font-weight: 500;
  font-size: 16px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}

:deep(.el-input__wrapper),
:deep(.el-select__wrapper) {
  box-shadow: none;
  border: 1px solid var(--el-border-color);
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover),
:deep(.el-select__wrapper:hover) {
  border-color: var(--el-color-primary);
}
</style>
